package com.spring.provideronetest.controller;

import com.spring.provideronetest.Utils.JsonResult;
import com.spring.provideronetest.Utils.JsonUtils;
import com.spring.provideronetest.Utils.RedisOperatorUtils;
import com.spring.provideronetest.entity.SysUser;
import com.spring.provideronetest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther zhangxiangtao
 * @date 2021/1/15
 */
@RestController
@RequestMapping("redis")
public class RedisController {
	@Autowired
	private StringRedisTemplate strRedis;

	@Autowired
	private RedisOperatorUtils redis;

	@RequestMapping("/test")
	public JsonResult test() {
		SysUser user = new SysUser();
		user.setId("100111");
		user.setUsername("spring boot");
		user.setPassword("abc123");
		user.setIsDelete(0);
		user.setRegistTime(new Date());
		strRedis.opsForValue().set("json:user", JsonUtils.objectToJson(user));

		return JsonResult.ok(user);
	}

	@RequestMapping("/getJsonList")
	public JsonResult getJsonList() {

		User user = new User();
		user.setId(18);
		user.setRealName("慕课网");
		user.setPassWord("123456");


		User u1 = new User();
		u1.setId(19);
		u1.setRealName("spring boot");
		u1.setPassWord("123456");

		User u2 = new User();
		u2.setId(17);
		u2.setRealName("hello spring boot");
		u2.setPassWord("123456");

		List<User> userList = new ArrayList<>();
		userList.add(user);
		userList.add(u1);
		userList.add(u2);

		redis.set("json:info:userlist", JsonUtils.objectToJson(userList), 2000);

		String userListJson = redis.get("json:info:userlist");
		List<User> userListBorn = JsonUtils.jsonToList(userListJson, User.class);

		return JsonResult.ok(userListBorn);
	}


}
